package com.hsy.crm.web.web.finance;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Response;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Account;
import com.hsy.crm.web.domain.vo.account.AccountPageVo;
import com.hsy.crm.web.domain.vo.account.PayerVo;
import com.hsy.crm.web.domain.vo.customer.CustomerPageVo;
import com.hsy.crm.web.domain.vo.vendor.VendorPageVo;
import com.hsy.crm.web.service.impl.AccountService;
import com.hsy.crm.web.service.impl.CustomerService;
import com.hsy.crm.web.service.impl.VendorService;
import com.hsy.crm.web.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/account")
public class AccountAction extends BaseAction {

    @Autowired
    AccountService accountService;

    @Autowired
    CustomerService customerService;

    @Autowired
    VendorService vendorService;

    @RequestMapping("/insert")
    public String insert(Account account) {
        account.setType(App.Account.NORMAL);
        account.setBalance(account.getInitBalance());
        return Response.body(Unified.newInstance(accountService.insert(account)));
    }

    @RequestMapping("/update")
    public String update(Account account) {
        return Response.body(Unified.newInstance(accountService.update(account)));
    }

    @RequestMapping("/delete")
    public String delete(Account account) {
        return Response.body(accountService.deleteAccount(account));
    }

    @RequestMapping("/select")
    public String select(Account account) {
        return Response.body(Unified.newInstance(accountService.select(account)));
    }

    @RequestMapping("/get")
    public String get(Account account) {
        return Response.body(Unified.newInstance(accountService.selectOne(account)));
    }

    @RequestMapping("/page")
    public String page(AccountPageVo accountPageVo) {
        return Response.body(accountService.page(accountPageVo));
    }

    @RequestMapping("/payerPage")
    public String payerPage(PayerVo payerVo) {
        if( payerVo.getRelationType() == 1 ){
            VendorPageVo vendorPageVo = new VendorPageVo();
            vendorPageVo.setCustomerId(payerVo.getCustomerId());
            vendorPageVo.setLoginCompanyId(payerVo.getLoginCompanyId());
            vendorPageVo.setComplex(payerVo.getComplex());
            vendorPageVo.setPage(payerVo.getPage());
            vendorPageVo.setLimit(payerVo.getLimit());
            return Response.body(vendorService.page(vendorPageVo));
        }else if( payerVo.getRelationType() == 2 ){
            CustomerPageVo customerPageVo = new CustomerPageVo();
            customerPageVo.setCustomerId(payerVo.getCustomerId());
            customerPageVo.setLoginCompanyId(payerVo.getLoginCompanyId());
            customerPageVo.setComplex(payerVo.getComplex());
            customerPageVo.setPage(payerVo.getPage());
            customerPageVo.setLimit(payerVo.getLimit());
            return Response.body(customerService.page(customerPageVo));
        }else{
            return null;
        }
    }

}
